package local;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashSet;

public class CreateVector {

	public static final int TWENTY_FILES = 1;
	public static final int TWENTY_FILES_WITH_INVERSE = 2;
	public static final int ALL_FILES = 3;
	public static final int ALL_FILES_WITH_INVERSE = 4;
	public static final int LOCAL = 5;
	public static int workingMode = LOCAL;
	private static String workName; 
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		if(workingMode == TWENTY_FILES){
			workName = "20files";
		}else if(workingMode == TWENTY_FILES_WITH_INVERSE){
			workName = "20filesWithInverse";
		}else if(workingMode == ALL_FILES){
			workName = "allFiles";
		}else if(workingMode == ALL_FILES_WITH_INVERSE){
			workName = "allFilesWithInverse";
		} else if (workingMode == LOCAL) {
			workName = "0";
		}
		HashSet<String> vector = new HashSet<String>();
		URL filePate = null;
		FileReader reader;
		try {
			int counter = 0;
			for(int i = 0 ; i <= 16 ; i++)
			{
				String fileNum = i<10 ? "0"+i : ""+i ;
				BufferedReader in = null;
				
				if (workingMode == LOCAL) {
					reader = new FileReader(workName + "/known-r-000" + fileNum);		
					in = new BufferedReader(reader);
				} else {
					filePate = new URL("https://s3.amazonaws.com/ahgfinal/result/" + workName + "/Step2/known-r-000" + fileNum);
					in = new BufferedReader(new InputStreamReader(filePate.openStream()));
					
				}
				
				String inputLine;
	
				boolean ignoreLine = true;
				while ((inputLine = in.readLine()) != null)
				{
					if(inputLine.equals("True") || inputLine.equals("False"))
					{
						counter++;
					}
					else if(ignoreLine)
					{
						ignoreLine = false;
					}
					else if(inputLine.isEmpty())
					{
						ignoreLine = true;
					}
					else
					{
						vector.add(inputLine);
					}
				}
				in.close();
			}
			System.out.println("num of pairs " + counter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		File f = new File(workName + "Vec.txt");
		
		try {
			FileOutputStream out = new FileOutputStream(f);
			System.out.println("num of features:" + vector.size());
			for (String string : vector) {
				out.write((string+"\n").getBytes());
			}
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
